﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace RescueRobotController.Panels
{
    public partial class RoboticArmPanel : DockContent
    {
        private Dictionary<string, NumericUpDown> numerics;

        // 下面的内容是每个面板必须要有的
        // *********** BEGIN ************* //
        // “发送AT指令”事件
        public delegate void ExistATCmdToSendHandler(ATCmd cmd);
        public event ExistATCmdToSendHandler SendATCmd;

        // 处理AT指令
        public void HandleATCmd(ATCmd cmd)
        {
            string target = cmd.target;
            string value = cmd.value;
            if (numerics.ContainsKey(target))
                numerics[target].Value = decimal.Parse(value);
        }
        // *********** END ************* //
        public RoboticArmPanel()
        {
            InitializeComponent();
            numerics = new Dictionary<string, NumericUpDown>
            {
                { "ARM_ANGEL1", numeric_angle1 },
                { "ARM_ANGEL2", numeric_angle2 },
                { "ARM_ANGEL3", numeric_angle3 },
                { "ARM_ANGEL4", numeric_angle4 },
                { "ARM_ANGEL5", numeric_angle5 },
                { "ARM_ANGEL6", numeric_angle6 }
            };

            foreach (string key in numerics.Keys )
            {
                numerics[key].ValueChanged += (object sender, EventArgs e) =>
                {
                    NumericUpDown numeric = (NumericUpDown)sender;
                    SendATCmd(new ATCmd()
                    {
                        type = ATCmdType.SET,
                        target = key,
                        value = numeric.Value.ToString()
                    });
                };
            }
        }
    }
}
